//1,新建文件   /pages/home/index.dart

//2,添加 material, page_content 依赖

import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutterapp/page/utils/common_toast.dart';
import 'package:flutterapp/page/utils/dio_http.dart';
import 'package:flutterapp/page/utils/string_is_null_or_entity.dart';

//3, 编写无状态组件

//有状态组件
class RegisterPage extends StatefulWidget {
  //构造函数
  const RegisterPage({Key key}) : super(key: key);

  @override
  RegisterPageState createState() => RegisterPageState();
}

class RegisterPageState extends State<RegisterPage> {
  //bool showPassword = false;
  var usernameController = TextEditingController();
  var passwordController = TextEditingController();
  var repeatPasswordController = TextEditingController();

  _registerHandler() async {
    var username = usernameController.text;
    var pass = passwordController.text;
    var repeatpass = repeatPasswordController.text;

    if (pass != repeatpass) {
      CommonToast.showToast('两次输入的密码不一致');
      return;
    }

    if (stringIsNullOrEmpty(username) || stringIsNullOrEmpty(pass)) {
      CommonToast.showToast('用户名或密码不能为空');
      return;
    }

    const url = '/api/user/register';
    var params = {"username": username, "pass": pass};
    var res = await DioHttp.of(context).post(url, params);
    //转json
    var resString = json.decode(res.toString());
    // print(resString);
    // print(res);
    bool flag = resString['flag'];
    String message = resString['message'];
    Object data = resString['data'] ?? '内部错误';
    print(flag);
    print(message);
    print(data);
    if (flag) {
      CommonToast.showToast('注册成功即将跳转');
      //延迟3秒跳转
      new Future.delayed(Duration(seconds: 3), () {
        Navigator.pushReplacementNamed(context, 'login');
      });
    } else {
      CommonToast.showToast('注册失败');
    }

    String str = '2343434';
    //判断2开头
    bool strbool = str.toString().startsWith('2');
    print(strbool);

    //print(resString.message);
    // print(resString.data);
  }

  @override
  Widget build(BuildContext context) {
    // 1,添加 Scaffaid

    //2,完成appBar 部分

    //3,完成body 部分

    //3.1 用户名

    //3.2密码

    //3.3登录按钮

    //3.4注册按钮

    //4.主体颜色
    //

    //5 测试
    return Scaffold(
      appBar: AppBar(
        centerTitle: true, // 标题居中
        title: Text("新用户注册"),
      ),
      body: SafeArea(
        minimum: EdgeInsets.all(30),
        child: ListView(
          children: [
            TextField(
              controller: usernameController,
              decoration: InputDecoration(labelText: '用户名', hintText: '请输入用户名'),
            ),
            Padding(
              padding: EdgeInsets.all(10),
            ),
            TextField(
              controller: passwordController,
              obscureText: true,
              decoration: InputDecoration(
                labelText: '密码',
                hintText: '请输入密码',
              ),
            ),
            Padding(
              padding: EdgeInsets.all(10),
            ),
            TextField(
              controller: repeatPasswordController,
              obscureText: true,
              decoration: InputDecoration(
                labelText: '确认密码',
                hintText: '请输入密码',
              ),
            ),
            Padding(
              padding: EdgeInsets.all(10),
            ),
            RaisedButton(
              color: Colors.green,
              child: Text('注册', style: TextStyle(color: Colors.white)),
              onPressed: () {
                // 点击逻辑
                print('asdfasdfasdf');
                _registerHandler();
              },
            ),
            Padding(
              padding: EdgeInsets.all(10),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Text('已有账号'),
                FlatButton(
                    onPressed: () {
                      Navigator.pushReplacementNamed(context, 'login');
                    },
                    child: Text(
                      '去登录~',
                      style: TextStyle(color: Colors.green),
                    ))
              ],
            )
          ],
        ),
      ),
    );
  }
}

//4,使用PageContent
